package com.example.myapplication.presenter.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.myapplication.App;

import java.util.ArrayList;
import java.util.List;


public class WiperListAdapter extends PagerAdapter {
    private static String TAG = "swiperListAdapter";
    private List<ImageView> imgs = new ArrayList<>();
    private List<ImageView> title;

    public WiperListAdapter(List<Integer> res, Context context) {
        initData(res, context);
    }

    private void initData(List<Integer> res, Context context) {
        App app = (App) context.getApplicationContext();
        int width = app.getwWidth();
        Log.i(TAG, "initData: "+width);
        for (int i = 0; i < res.size(); i++) {
            Bitmap bp = BitmapFactory.decodeResource(context.getResources(), res.get(i));
            ImageView iv = new ImageView(context);
            iv.setImageResource(res.get(i));
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(width, width * bp.getHeight() / bp.getWidth());
            iv.setLayoutParams(lp);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            imgs.add(iv);
        }
    }

    @Override
    public int getCount() {
        return imgs.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(imgs.get(position));
        return imgs.get(position);
    }


    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(imgs.get(position));
    }


}

